﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Data;
using blackhouse.WebNemo.Business.Users;
using blackhouse.WebNemo.Business.Translate;

namespace blackhouse.WebNemo.Business {
    public abstract class Factory : blackhouse.InstanceFactory<Factory> {

        #region INSTANCE

        public static Factory Instance() {
            return Instance("WebNemoFactory", typeof(SqlFactory));
        }

        #endregion

        #region Methods

        public ITranslate GetTranslates() {
            return new DaTranslate(this.GetDataAccess());
        }

        public virtual IGroup GetGroup(string groupSystemName) {
            IGroupOperations opera = this.GetGroupOperators();
            int groupId = opera.GetGroupId(groupSystemName);
            if (groupId == 0)
                throw new Exception("Grupa " + groupSystemName + " nie została odnaleziona");
            return GetGroup(groupId);
        }

        public virtual IGroup GetGroup(int groupId) {
            return new DaGroup(this.GetDataAccess(), groupId);
        }

        public virtual IGroup CreateGroup() {
            return new DaGroup(this.GetDataAccess());
        }

        public virtual IGroupOperations GetGroupOperators() {
            return new DaGroup(this.GetDataAccess());
        }

        public GroupCollection GetGroups() {
            return new GroupCollection(new List<IGroup>(this.GetGroupOperators().GetGroups()));
        }

        public INemoUser GetUser(int userId) {
            return NemoUser.GetUser(userId);
        }

        public INemoUser GetCurrentUser() {
            return NemoUser.GetCurrentUser();
        }

        protected virtual INemoUser CreateUser() {
            return new DaNemoUser(this.GetDataAccess());
        }

        public virtual SplitColumns GetSplitColumns(IGroup group) {
            return new SplitColumns(group, this.GetDataAccess());
        }

        #endregion

        #region UserDataAccess

        private static INemoUser nemoUser;

        private INemoUser NemoUser {
            get {
                if (nemoUser == null)
                    nemoUser = this.CreateUser();
                return nemoUser;
            }
        }

        #endregion

        #region GroupDataAccess

        //private static IDataAccess dataAccess;

        //private static IGroupOperations groupOperations;

        //internal IDataAccess DataAccess {
        //    get {
        //        if (dataAccess == null)
        //            dataAccess = this.GetDataAccess();
        //        return dataAccess;
        //    }
        //}

        //private IGroupOperations GroupOperations {
        //    get {
        //        if (groupOperations == null)
        //            groupOperations = new DaGroup(this.DataAccess);
        //        return groupOperations;
        //    }
        //}

        public abstract IDataAccess GetDataAccess();

        #endregion

    }
}
